﻿Public Class CodeConverter
    Public Shared Function FStoVB(code As String, classname As String) As String
        Dim ret As String
        ret = "Public Class " + classname.Replace("+", "_").Replace("-", "_").Replace("*", "_").Replace("/", "_").Replace("\", "_").Replace("-", "_") + vbLf

        Dim stackg = False
        ret += "Shared Sub Main()" + vbLf


        For Each ln In code.Split(vbLf)
            ln = ln.ToLower
            If ln.StartsWith("##") Then
                ret += ln.Replace("##", "'") + vbLf
            ElseIf ln.StartsWith("push ") Then
                If stackg Then
rew:
                    ret += ln.Replace("push ", "stack.Push(") + ")"
                Else
                    ret += "Dim stack As New Stack(Of Object)()" + vbLf
                    stackg = True
                    GoTo rew
                End If
            ElseIf ln.StartsWith("push$ ") Then
                If stackg Then
rews:
                    ret += ln.Replace("push$ ", "stack.Push(""") + """)"
                Else
                    ret += "Dim stack As New Stack(Of Object)()" + vbLf
                    stackg = True
                    GoTo rews
                End If
            ElseIf ln = "prt" Then
                If Not stackg Then
                    Throw New System.NullReferenceException("Undefined stack")
                Else
                    ret += "For Each abc In stack" + vbLf
                    ret += "Console.WriteLine(abc)" + vbLf
                    ret += "Next" + vbLf
                End If
            ElseIf ln = "hlt" Then
                ret += "Console.ReadLine()" + vbLf
                ret += "End" + vbLf
            ElseIf ln = "wfk" Then
                ret += "Console.ReadLine()" + vbLf
            ElseIf ln = "cls" Then
                ret += "Console.Clear()" + vbLf
            ElseIf ln.StartsWith("wait ") Then
                ret += ln.Replace("wait ", "System.Threading.Thread.Sleep(") + ")"
            ElseIf ln.StartsWith("show ") Then
                Dim res As String = ln.Replace("show ", "")
                Dim inch = False
                Dim chn As Int32
                Dim message As String
                For Each ch As Char In res
                    If Not inch Then
                        If ch = "#" Then
                            inch = True
                        Else
                            message = message & ch
                        End If
                    Else
                        If IsNumeric(ch) Then
                            chn = chn & ch
                        ElseIf ch = "#" Then
                            message = message + Chr(chn)
                            chn = Nothing
                            inch = False
                        Else
                            Exit Function
                        End If
                    End If
                Next
                ret += "MsgBox("""
                ret += message
                ret += """)"
            End If
        Next



        ret = ret + "End Class"
        Return ret
    End Function





End Class
